package mess;

public class LeeCode_63 {
    public static void main(String[] args) {
        System.out.println(uniquePathsWithObstacles(new int[][]{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}));
      /*  System.out.println(uniquePathsWithObstacles(new int[][]{{0, 0}, {1, 0}}));
        System.out.println(uniquePathsWithObstacles(new int[][]{{0,0,0,0,0}, {0,0,0,0,1},
                {0,0,0,1,0},{0,0,0,0,0}}));*/
        //System.out.println(uniquePathsWithObstacles(new int[][]{{1, 0}}));

    }
    private static int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        if (obstacleGrid[0][0] == 1 || obstacleGrid[m - 1][n - 1] == 1)
            return 0;
        boolean flag1 = true;
        boolean flag2 = true;
        int[][] result = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (i == 0 && obstacleGrid[i][j] == 1){
                    flag1 = false;
                    continue;
                }
                if (j == 0 && obstacleGrid[i][j] == 1){
                    flag2 = false;
                    continue;
                }
                if (i == 0 && flag1){
                    result[i][j] = 1;
                    continue;
                }
                if (j == 0 && flag2){
                    result[i][j] = 1;
                    continue;
                }
                if (obstacleGrid[i][j] != 1 && j > 0 && i > 0)
                    result[i][j] = result[i - 1][j] + result[i][j - 1];
            }
        }
        return result[m - 1][n - 1];
    }
}
